/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package algorithm;

import datastructure.Pair;
import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author Winzelric
 */
public class KNearestNeighbourModel {
  
  private ArrayList<ArrayList<String>> data;
  private int k;
  
  public KNearestNeighbourModel(ArrayList<ArrayList<String>> data, int k) {
    this.data = data;
    this.k = k;
  }
  
  public void setK(int k){
    this.k = k;
  }
  
  public String executeInstance (ArrayList<String> instance) {
    int chosen_index = 0, temp_distance = 0;
    ArrayList<Pair<Integer,Integer>> list_of_distance = new ArrayList <Pair<Integer,Integer>>();
    for (int row = 0; row < data.size(); row++) {
      temp_distance = 0;
      for (int attribute = 1; attribute < instance.size(); attribute++) {
        if (!instance.get(attribute).equals(data.get(row).get(row))) {
          temp_distance++;
        } 
      }
      Pair<Integer,Integer> distance = Pair.makePair(temp_distance, row);
    }
    
    Collections.sort(list_of_distance);
    
    int count1 = 0, count0 = 0;
    for (int row = 0; row < k; row++) {
      int index = list_of_distance.get(row).second;
      if ("1".equals(data.get(index).get(0))) {
        count1++;
      } else {
        count0++;
      }
    }
    
    return (count1++ > count0) ? "1" : "0";
  }
  
}
